First, we read in the data and set it up for analysis. The data is mostly cleaned, but we need a subset for calculating correlation, we need to change some data to be categorical, some data to be numerical, and we need to fix the dates so that they aren’t read in as characters.
Without doing anything, our dataset is as follows:
After cleaning, our main dataset is described below:
Our secondary dataset (used to measure correlation) is described below:
Scatter plot for price and number of reviews:
library(ggplot2)
library(ggpubr)
ggplot(airbnb, aes(x=price, y=number_of_reviews,)) + ggtitle("Number of Reviews vs Price Scatter Plot") + xlab("Price ($)") + ylab("Number Of Reviews") + geom_point(size = 1, shape = 18, color = "black") + geom_smooth(method = lm, se = FALSE, color = "yellow", size = 1.2) + theme_bw() + stat_cor(method = "pearson", label.x = 7500 )
library(ggplot2)
ggplot(airbnb, aes(price, factor(neighbourhood_group))) + geom_boxplot(width = 0.7, color = "black", fill = c("light green", "pink","light blue", "yellow", "red")) +labs(title = "Neighbourhood group vs Price Box plot", x = "Price", y = "Neighbourhood")
library(ggplot2)
ggplot(airbnb, aes(price, factor(room_type))) + geom_boxplot(width = 0.7, color = "black", fill = c("light green", "yellow","light blue")) +labs(title = "Room type vs Price Box plot", x = "Price", y = "Room Type")
library(ggmap)
library(tmaptools)
library(tidyr)
ggmap(get_stamenmap(rbind(as.numeric(paste(geocode_OSM("New York")$bbox))), zoom = 10)) +
geom_point(data = airbnb, aes(x = longitude, y = latitude, colour = neighbourhood_group, size = price), alpha = 0.2)
# create subset just for aggregating by mean
airbnb_map <- airbnb[ , c(6, 7, 8, 10)]
airbnb_map_means <- aggregate(.~neighbourhood, airbnb_map, mean)
# create subset for aggregating by count
airbnb_count <- airbnb_map
airbnb_count$count <- 1
airbnb_count <- airbnb_count[, c(1,5)]
airbnb_counter <- aggregate(.~neighbourhood, airbnb_count, sum)
# create full dataset from both subsets
airbnb_map_full <- cbind(airbnb_counter, airbnb_map_means)
# check that union occured correctly, then drop extra neighborhood value
all.equal(airbnb_map_full[, 1], airbnb_map_full[, 3])
## [1] TRUE
airbnb_map_full <- airbnb_map_full[, -3]
library(ggmap)
library(tmaptools)
library(tidyr)
ggmap(get_stamenmap(rbind(as.numeric(paste(geocode_OSM("New York")$bbox))), zoom = 10)) +
geom_point(data = airbnb_map_full, aes(x = longitude, y = latitude, colour = price, size = count), alpha = 0.5) + scale_colour_gradientn(colours=rainbow(3))
# Section IV: Correlation and ANOVA Tests
loadPkg("faraway")
loadPkg("corrplot")
xkabledply(cor(airbnb_cor))
| price | minimum_nights | number_of_reviews | reviews_per_month | calculated_host_listings_count | availability_365 | |
|---|---|---|---|---|---|---|
| price | 1.0000 | 0.0428 | -0.0480 | NA | 0.0575 | 0.0818 |
| minimum_nights | 0.0428 | 1.0000 | -0.0801 | NA | 0.1280 | 0.1443 |
| number_of_reviews | -0.0480 | -0.0801 | 1.0000 | NA | -0.0724 | 0.1720 |
| reviews_per_month | NA | NA | NA | 1 | NA | NA |
| calculated_host_listings_count | 0.0575 | 0.1280 | -0.0724 | NA | 1.0000 | 0.2257 |
| availability_365 | 0.0818 | 0.1443 | 0.1720 | NA | 0.2257 | 1.0000 |
airbnb_corplot = cor(airbnb_cor, use = "complete.obs")
corrplot(airbnb_corplot, method = "circle")
No strong correlations with price, but minimum_nights and availability_365, number_of_reviews and availability_365, and calculated_host_listings_count and availability_365 show some evidence of positive correlation.
plot(x=airbnb_cor$reviews_per_month, y=airbnb_cor$number_of_reviews)
cor.test(x=airbnb_cor$reviews_per_month, y=airbnb_cor$number_of_reviews)
##
## Pearson's product-moment correlation
##
## data: airbnb_cor$reviews_per_month and airbnb_cor$number_of_reviews
## t = 130, df = 38841, p-value <2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.543 0.557
## sample estimates:
## cor
## 0.55
As expected, correlated since reviews per month is a function of total number of reviews so do not need to look at both.
plot(y=airbnb$number_of_reviews, x=airbnb$price)
cor.test(y=airbnb$number_of_reviews, x=airbnb$price)
##
## Pearson's product-moment correlation
##
## data: airbnb$price and airbnb$number_of_reviews
## t = -11, df = 48893, p-value <2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.0568 -0.0391
## sample estimates:
## cor
## -0.048
No evidence of strong (linear) correlation, but evidence of an inverse relationship between price and reviews (higher price, fewer reviews–possibly because of fewer stays, for which review number is probably a good proxy)
#anova test for price and neighborhood groups
anova_price_group = aov(price ~ neighbourhood_group, data=airbnb)
anova_price_group
## Call:
## aov(formula = price ~ neighbourhood_group, data = airbnb)
##
## Terms:
## neighbourhood_group Residuals
## Sum of Squares 7.96e+07 2.74e+09
## Deg. of Freedom 4 48890
##
## Residual standard error: 237
## Estimated effects may be unbalanced
summary(anova_price_group) -> sum_anova_price_group
xkabledply(sum_anova_price_group, title = "ANOVA result summary for Neighborhood Groups")
| Df | Sum Sq | Mean Sq | F value | Pr(>F) | |
|---|---|---|---|---|---|
| neighbourhood_group | 4 | 7.96e+07 | 19897739 | 355 | 0 |
| Residuals | 48890 | 2.74e+09 | 56051 | NA | NA |
tukeyAoV_pg <- TukeyHSD(anova_price_group)
tukeyAoV_pg
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = price ~ neighbourhood_group, data = airbnb)
##
## $neighbourhood_group
## diff lwr upr p adj
## Brooklyn-Bronx 36.89 16.81 57.0 0.000
## Manhattan-Bronx 109.38 89.34 129.4 0.000
## Queens-Bronx 12.02 -9.33 33.4 0.539
## Staten Island-Bronx 27.32 -11.42 66.1 0.305
## Manhattan-Brooklyn 72.49 66.17 78.8 0.000
## Queens-Brooklyn -24.87 -34.58 -15.2 0.000
## Staten Island-Brooklyn -9.57 -43.32 24.2 0.938
## Queens-Manhattan -97.36 -106.99 -87.7 0.000
## Staten Island-Manhattan -82.06 -115.79 -48.3 0.000
## Staten Island-Queens 15.29 -19.23 49.8 0.746
#anova test for price and room type
anova_price_room = aov(price ~ room_type, data=airbnb)
anova_price_room
## Call:
## aov(formula = price ~ room_type, data = airbnb)
##
## Terms:
## room_type Residuals
## Sum of Squares 1.85e+08 2.63e+09
## Deg. of Freedom 2 48892
##
## Residual standard error: 232
## Estimated effects may be unbalanced
summary(anova_price_room) -> sum_anova_price_room
xkabledply(sum_anova_price_room, title = "ANOVA result summary for Room Type")
| Df | Sum Sq | Mean Sq | F value | Pr(>F) | |
|---|---|---|---|---|---|
| room_type | 2 | 1.85e+08 | 92512441 | 1717 | 0 |
| Residuals | 48892 | 2.63e+09 | 53892 | NA | NA |
tukeyAoV_pr <- TukeyHSD(anova_price_room)
tukeyAoV_pr
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = price ~ room_type, data = airbnb)
##
## $room_type
## diff lwr upr p adj
## Private room-Entire home/apt -122.0 -127 -117.02 0.000
## Shared room-Entire home/apt -141.7 -158 -125.33 0.000
## Shared room-Private room -19.7 -36 -3.27 0.014